package cn.anecansaitin.inet.mixins;

import cn.anecansaitin.inet.TranslateHelper;
import mcjty.lib.McJtyLib;
import mcjty.lib.gui.GuiSideWindow;
import mcjty.lib.gui.GuiStyle;
import mcjty.lib.gui.Window;
import mcjty.lib.gui.widgets.Button;
import mcjty.lib.gui.widgets.Panel;
import mcjty.lib.gui.widgets.Widgets;
import mcjty.lib.preferences.PreferencesProperties;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraftforge.network.simple.SimpleChannel;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(GuiSideWindow.class)
public abstract class GuiSideWindowMixin {
    @Shadow
    private GuiStyle style;
    @Shadow
    private Button guiButton;
    @Shadow
    private Window sideWindow;

    @Shadow
    protected abstract void help(Minecraft mc);
    @Shadow
    protected abstract void changeStyle(SimpleChannel network);
    @Shadow
    protected abstract void setStyleTooltip();

    @Inject(method = "initGui", at = @At("HEAD"), remap = false, cancellable = true)
    private void mixin$initGui(Minecraft mc, Screen gui, int guiLeft, int guiTop, int xSize, int ySize, CallbackInfo ci) {
        style = McJtyLib.getPreferencesProperties(mc.player).map(PreferencesProperties::getStyle).orElse(GuiStyle.STYLE_FLAT_GRADIENT);

        Button helpButton = Widgets.button(1, 1, 16, 16, "?")
                .tooltips(TranslateHelper.getText("inet.gui_side_window.button.help.tooltip"))
                .event(() -> help(mc));
        guiButton = Widgets.button(1, 19, 16, 16, "s")
                .event(() -> changeStyle(McJtyLib.networkHandler));
        setStyleTooltip();
        Panel sidePanel = Widgets.positional().children(guiButton, helpButton);
        int sideLeft = guiLeft + xSize;
        int sideTop = guiTop + (ySize - 20) / 2 - 8;
        sidePanel.bounds(sideLeft, sideTop, 20, 40);
        sideWindow = new Window(gui, sidePanel);
        ci.cancel();
    }

    @Inject(method = "setStyleTooltip", at = @At("HEAD"), remap = false, cancellable = true)
    private void mixin$setStyleTooltip(CallbackInfo ci) {
        guiButton.tooltips(TranslateHelper.getText("inet.gui_side_window.button.style.tooltip"), style.getStyle());
        ci.cancel();
    }
}
